<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\ShopSecurityDeposit as mShopSecurityDeposit;
use Biz\model\ShopBasicInfo as mShopBasicInfo;
use Biz\model\ShopCompanyInfo as mShopCompanyInfo;
use Biz\model\ShopPersonalInfo as mShopPersonalInfo;
use Biz\tools\Mail;

$basicInfoList = mShopBasicInfo::select('*', ['status' => mShopBasicInfo::STATUS_OPENED, 'uid[!]' => \Biz\model\GoodsBasicInfo::$testShopIds]);
$uids = array_column($basicInfoList, 'uid');
$basicInfoList = array_column($basicInfoList, null, 'uid');
$companyShopIds = [];
$personalShopIds = [];
foreach ($basicInfoList as $row) {
    if ($row['type'] == mShopBasicInfo::TYPE_COMPANY) {
        array_push($companyShopIds, $row['uid']);
    } else if ($row['type'] == mShopBasicInfo::TYPE_PERSONAL) {
        array_push($personalShopIds, $row['uid']);
    }
}

$companyList = mShopCompanyInfo::getInfoList($companyShopIds);

$personalList = mShopPersonalInfo::getInfoList($personalShopIds);

$depositList = mShopSecurityDeposit::getDepositList($uids);

$date = date('Ym');
$title = [
    '商家uid',
    '商家名称',
    '商家类型',
    '商家实名信息',
    '保证金额度',
    '保证金余额',
];
$dir = "/tmp/";
$file_path = $dir . "deposit-" . $date . ".csv";
$fp = fopen($file_path, 'w');
fputcsv($fp, $title);

foreach ($depositList as $uid => $row) {
    $uid = $row['uid'];
    $type = ($basicInfoList[$row['uid']]['type'] == mShopBasicInfo::TYPE_COMPANY) ? '企业' : '个人';
    $realname = ($basicInfoList[$row['uid']]['type'] == mShopBasicInfo::TYPE_COMPANY) ? $companyList[$uid]['name'] : $personalList[$uid]['realname'];
    $content = [
        $uid,
        $basicInfoList[$uid]['name'],
        $type,
        $realname,
        $row['amount'],
        $row['balance']
    ];
    fputcsv($fp, $content);
}

fclose($fp);
$sendMail = [
    'tangyiwan@haohaozhu.com',
    'verawang@haohaozhu.com',
];
Mail::send($sendMail, $date . " 商户的保证金余额", $date . '商户的保证金余额', $file_path);

